package controllers.manage.product;

import com.google.gson.Gson;
import controllers.BaseController;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import play.Logger;
import play.mvc.Controller;
import play.mvc.With;
import product.type.ProductType;
import util.common.ConvertUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 产品类别管理
 * Created by hanbin on 17/4/6.
 */
@With(BaseController.class)
public class ProductTypeCtrl extends Controller{

    /**
     * 跳转到产品类别管理页面
     */
    @SkipAuth
    public static void index(){
        render();
    }

    //加载数据,默认展开两层
    public static void loadTreeData(long id) {
        List<ProductType> productTypeList = new ArrayList<>();
        if(id == 0){
            List<ProductType> typeList = ProductType.loadChildType(id);
            if(typeList != null){
                for(ProductType productType : typeList){
                    productTypeList.add(productType);
                    List<ProductType> childList = ProductType.loadChildType(productType.id);
                    productTypeList.addAll(childList);
                }
            }

        }else {
            productTypeList = ProductType.loadChildType(id);
        }

        renderJSON(ConvertUtil.toJson(productTypeList));
    }

    /**
     * 跳转到新增产品类别页面
     */
    public static void add(){
        render();
    }

    /**
     * 跳转到修改产品类别页面
     */
    public static void modify(long id, Integer pageNumber){
        ProductType productType = ProductType.findById(id);
        render(productType, pageNumber);
    }


    /**
     * 创建产品类别
     */
    public static void create(ProductType productType){

        Merchant merchant = Secure.getLoginUser().merchant;
        productType.merchant = merchant;

        productType.deleted = DeletedStatus.UN_DELETED;
        productType.save();
        flash.put("msg" , "产品类别创建成功");
        redirect("/BS0018/admin/product/type/index/1");
    }

    /**
     * 修改产品类别
     */
    public static void update(Long id, ProductType productType, Integer pageNumber){
        ProductType.update(id, productType);
        flash.put("msg" , "产品类别修改成功");
        redirect("/BS0018/admin/product/type/index/"+pageNumber);
    }

    /**
     * 删除产品类别
     */
    public static void delete(long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        ProductType productType = ProductType.findById(id);
        //判断产品类型是否被使用
        if(ProductType.checkIsUsed(id)){
            flash.put("msg" , "该产品类别正在被使用,无法删除!");
            redirect("/BS0018/admin/product/type/index/"+pageNumber);
        }

        productType.deleted=DeletedStatus.DELETED;
        productType.save();
        flash.put("msg" , "产品类别删除成功");
        redirect("/BS0018/admin/product/type/index/"+pageNumber);
    }

    public static void saveProductTypeDetail(ProductType productType) {
        Logger.info("LOGGER   productType.name ====================:%s",productType.name);
        Logger.info("LOGGER   productType ====================:%s",new Gson().toJson(productType));
        if (productType != null && productType.id != null) {
            productType.update(productType);
        } else if (productType != null) {
            //叶子节点
            productType.isLeaf = true;
            productType.deleted = DeletedStatus.UN_DELETED;
            //创建人
            productType.creator = Secure.getLoginUser();
            productType.createAt = new Date();
            productType.save();
            //判断父节点是否存在,
            ProductType parentPro;
            Long parentId = productType.parentTypeId;
            parentPro = ProductType.findByTypeId(parentId);
            if (parentId != null && parentId > 0) {
                //判断父节点是否是叶子节点
                if (parentPro != null && parentPro.isLeaf == true) {
                    parentPro.isLeaf = false;
                    parentPro.save();
                }
                //节点路径
                if (productType != null && productType.parentTypeId > 0) {
                    Logger.info(" productType.id1  ===========  : %s" ,  productType.id );
                    productType.nodeIdPath = parentPro.nodeIdPath  + productType.id + "/";
                }

            }else{
                Logger.info(" productType.id ===========  : %s" ,  productType.id );
                productType.nodeIdPath = "/" + productType.id + "/";
            }
            productType.save();
        }
        flash.put("msg", "保存成功!");
        redirect("/BS0018/admin/product/type/index");

    }

    /**
     * 删除产品类别
     */
    public static void deleteProType(long id){
        Logger.info(" productType.id ===========  : %s" ,  id );
        ProductType productType = ProductType.findById(id);
        //判断产品类型是否被使用
        if(ProductType.checkIsUsed(id)){
            flash.put("msg" , "该产品类别正在被使用,无法删除!");

        }else{
            productType.deleted=DeletedStatus.DELETED;
            productType.save();
            flash.put("msg" , "产品类别删除成功");
        }
        redirect("/BS0018/admin/product/type/index");
    }

}
